package com.hxh.generater.vue.springboot3_vue3.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.common.springboot3_vue3.Springboot3Vue3Config;
import com.hxh.generater.springBoot.util.GetAllTables;
import com.hxh.generater.vue.genBase.InitVueProject;
import com.hxh.util.DirClone;
import com.hxh.util.GenUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class InitSb3Vue3Project extends InitVueProject {
    public static final String FILE_PATH_PRE_BASE = Constants.vueTemplatePath + "springboot3_vue3\\template\\";
    public static final String FILE_PATH_PRE = FILE_PATH_PRE_BASE + "base\\";

    private static void generationConfigEnv(Project project) throws IOException {
        //处理端口
        File file = GenUtil.newVueFile(project, "", ".env.development");
        try {
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(FILE_PATH_PRE + ".env.development");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#baseUrl#", project.getFrontendProject().getBaseUrl());
                    line = line.replaceAll("#projectPort#", project.getFrontendProject().getProjectPort());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void generationProjectName(Project project,String fileName) throws IOException {
        //处理端口
        File file = GenUtil.newVueFile(project, "", fileName);
        try {
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(FILE_PATH_PRE + fileName);
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#projectName#", project.getName());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void generationAllProjectName(Project project) throws IOException {
        generationProjectName(project,"index.html");
        generationProjectName(project,"package.json");
    }

    private static void generationBuildDir(Project project) throws IOException {
        String sourcePath = FILE_PATH_PRE;
        String destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + File.separator;
        DirClone dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);
    }

    /**
     * 初始化表的api和vue文件
     *
     * @param project
     * @param initExtendTable
     */
    @Override
    public void generationAllApiAndView(Project project, boolean initExtendTable) throws Exception {
        generationBuildDir(project);
        List<ClassInfo> classInfos = new ArrayList<>();
        if (initExtendTable) {
            classInfos = GetAllTables.generationAllTableInfo(project, Springboot3Vue3Config.springboot3Vue3TableList);
        }
        for (ClassInfo classInfo : classInfos) {
            GenSb3Vue3Vue genSb3Vue3Vue = new GenSb3Vue3Vue();
            genSb3Vue3Vue.generationView(project, classInfo);
            genSb3Vue3Vue.generationApi(project, classInfo);
        }
    }

    /**
     * 初始化系统基础文件
     *
     * @param project
     */
    @Override
    public void initSystemBase(Project project) throws Exception {
        generationBuildDir(project);
        generationConfigEnv(project);
        generationAllProjectName(project);
    }

    /**
     * @param project
     * @return void
     * @author HXH
     * @description: 可选组件
     * @date 2024/5/21
     */
    @Override
    public void initAddition(Project project) {

    }
}
